<?php

declare(strict_types=1);

namespace app\admin\controller\science;


use app\common\service\UploadService;
use think\response\Json;
use think\response\View;
use app\admin\controller\Controller;

class Science extends Controller
{
    /**
     * 重写中间件配置，科普文章列表API不需要认证
     */
    protected array $middleware = ['operLog'];
    
    /**
     * @var \app\admin\service\science\ScienceService
     */
    protected $service = null;

    public function initialize()
    {
        parent::initialize();
        $this->service = new \app\admin\service\science\ScienceService();
    }

    /**
     * 列表
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function list(): Json
    {
        return success($this->service->list());
    }
    
    /**
     * 渲染列表视图
     * @return View
     */
    public function render(): View
    {
     
        return view('science/list');
    }

    public function detail(int $id): Json
    {
        return success($this->service->detail($id));
    }

    public function create(): Json
    {
        return success('创建成功', $this->service->create(request()));
    }

    public function update(int $id): Json
    {
        return success('更新成功', $this->service->update($id, request()));
    }

    public function delete( ): Json
    {
        $id = request()->param('id', 0, 'intval');
        return $this->service->delete($id);
    }

    public function select(): Json
    {
        return success($this->service->select());
    }

    /**
     * 更新状态
     * @param int $id
     * @param int $status
     * @return Json
     */
    public function status(int $id, int $status): Json
    {
        return success('操作成功', $this->service->updateStatus($id, $status));
    }

    /**
     * 上传图片
     */
    public function image()
    {
        $groupId = request()->post('group_id',0);
        $res = UploadService::upload('image',$groupId);
        return success($res);
    }

    public function selects(): Json
    {
        return success((new \app\admin\service\user\UserService())->select());
    }

    public function details(int $id): Json
    {
        return success((new \app\admin\service\user\UserService())->detail($id));
    }
}